package com.taobao.schedule.checker;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;

import com.taobao.schedule.monitor.ScheduleMonitor;
import com.taobao.schedule.utils.TextUtil;

/**
 * 用于校验schedule的日志异常数
 * 
 * @author Livis
 * 
 */
public class ScheduleExceptionChecker extends GenericChecker {
	
	private static Logger log = Logger.getLogger(ScheduleExceptionChecker.class);
	
	@Override
	public void check() {
		try {
			//String shell = TextUtil.getCurrentShell();
			//Process p = Runtime.getRuntime().exec(shell);
			Process p = Runtime.getRuntime().exec(ScheduleMonitor.testShell);
			BufferedReader bufferedReader = new BufferedReader(
					new InputStreamReader(p.getInputStream()), 1024);
			p.waitFor();
			String line = null;
			StringBuffer stringBuffer = new StringBuffer();
			// 读取Shell的输出内容，并添加到stringBuffer中
			while (bufferedReader != null
					&& (line = bufferedReader.readLine()) != null) {
				stringBuffer.append(line).append("\r\n");
			}
			
			if(bufferedReader!=null){
				bufferedReader.close();
			}
			
			String result = stringBuffer.toString();
			log.warn("执行结果为：["+result+"]");
			log.warn("旺旺告警用户有："+this.getUsers());
			log.warn("手机告警用户有："+this.getMobiles());
			if(this.getUsers()!=null){
				this.alertByWW(result);
			}
			if(this.getMobiles()!=null){
				this.alertByMobile(result);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
